import 'package:flutter/material.dart';
import 'package:core_package/core_package.dart';
import 'package:flutter_app_unionad/core/configuration.dart';
import 'package:flutter_app_unionad/request/login_request.dart';
import 'package:flutter_app_unionad/ui/basice/base_center_alert_widget.dart';
import 'package:flutter_app_unionad/ui/basice/input_separator_widget.dart';
import 'package:flutter_app_unionad/ui/basice/my_button.dart';
import 'package:flutter_app_unionad/view_model/change_device_event.dart';

//绑定设备
class BindingFacilitySendCodeWidget extends BaseCloseAlertWidget {
  final String? device;
  final String? mobile;
  String? allMobile;

  BindingFacilitySendCodeWidget(this.device, this.mobile);

  @override
  Widget buildContent(BuildContext context) {
    return Column(
      children: [
        MySizeBox(height: 30),
        Text(
          "绑定设备号认证手机号",
          style: TextStyle(
            color: MyColors.color_27_27_27,
            fontSize: MyFont.font16,
            fontWeight: MyFontWeight.bold,
          ),
        ),
        MySizeBox(height: 25),
        Padding(
          padding: EdgeInsets.symmetric(horizontal: 69.px),
          child: Row(
            children: [
              Text(
                '手机号：',
                style: TextStyle(
                  color: MyColors.color_27_27_27,
                  fontSize: MyFont.font16,
                  fontWeight: MyFontWeight.bold,
                ),
              ),
              Expanded(child: SizedBox()),
              Text(
                mobile??"",
                style: TextStyle(
                  color: MyColors.color_27_27_27,
                  fontSize: MyFont.font16,
                  fontWeight: MyFontWeight.bold,
                ),
              ),
            ],
          ),
        ),
        MySizeBox(height: 20),
        Padding(
          padding: EdgeInsets.symmetric(horizontal: 69.px),
          child: InputSeparatorWidget(
            valueChange: (value) {
              allMobile = value;
            },
            title: "",
            keyboard: TextInputType.number,
            placeholder: "请输入完整手机号",
            titleWidth: 0,
            inputFont: MyFont.font16,
          ),
        ),
        MySizeBox(height: 30),
        MyButton(
          title: "确定",
          radius: 1.px,
          onTap: () {
            bindingDevice(context);
          },
          width: 133.px,
        )
      ],
    );
  }

  void bindingDevice(BuildContext context) {
    FocusScope.of(context).requestFocus(FocusNode());
    if (device ==null || device!.stringIsEmpty) {
      ShowToastWidget.showToast(msg: "请输入设备号");
      return;
    }
    if (!isPhoneString(allMobile)) {
      ShowToastWidget.showToast(msg: "请输入正确的手机号");
      return;
    }

    ProgressDialog.showProgress(context);
    LoginRequest.bindingDevice(devId: device, mobile: allMobile).then((value) {
      ProgressDialog.dismiss(context);
      if (value != null) {
        // UserCacheUil.setString(Configuration.deviceId, value);
        eventBus.fire(ChangeDeviceEvent(true));
        NavigatorUtil.pop(context);
      }
    });
  }
}
